package com.qf.hospital.handler;

import com.qf.hospital.VO.R;
import com.qf.hospital.enums.HospitalExceptionEnum;
import com.qf.hospital.exception.HosptialException;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author 何星宇
 * @version V1.0
 * @Date 2021/10/15 15:41
 */

@RestControllerAdvice
public class HospitalExceptionHandler {

    //捕获自定义异常
    @ExceptionHandler(HosptialException.class)
    public R handler(HosptialException e){
        return R.error(e.getCode(), e.getMessage());
    }

    //捕获认证失败异常（密码错误）
    @ExceptionHandler(IncorrectCredentialsException.class)
    public R incorrectCredentialsHandler(){
        return R.error(HospitalExceptionEnum.USERNAME_PASSWORD_ERROR);
    }

    //捕获账户异常（账户错误）
    @ExceptionHandler(UnknownAccountException.class)
    public R unknownAccountExceptionHandler(){
        return R.error(HospitalExceptionEnum.USERNAME_PASSWORD_ERROR);
    }

    //捕获认证错误异常
    @ExceptionHandler(AuthenticationException.class)
    public R authenticationExceptionHandler(){
        return R.error(HospitalExceptionEnum.USERNAME_PASSWORD_ERROR);
    }
}
